{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "7712863b",
   "metadata": {},
   "source": [
    "# 1. 导入数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "id": "3964bc67",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "from sklearn.datasets import load_digits\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.neighbors import KNeighborsClassifier\n",
    "from sklearn.metrics import accuracy_score\n",
    "import warnings#导入warnings库该库用于控制Python程序中的警告信息，这里出现警告但是不影响程序的运行采用的（王汇汇418）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "id": "c5b30e23",
   "metadata": {},
   "outputs": [],
   "source": [
    "warnings.filterwarnings(\"ignore\", category=FutureWarning)#忽略警告下方出现警告但是不影响运行"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "id": "1359ec53",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 加载手写数字数据集\n",
    "digits = load_digits()\n",
    "X = digits.data\n",
    "y = digits.target"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9f6ff828",
   "metadata": {},
   "source": [
    "# 2. 展示手写数字灰度图"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "id": "83953368",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0.  0.  0. 14. 14.  1.  0.  0.]\n",
      " [ 0.  0.  7. 16. 10.  2.  0.  0.]\n",
      " [ 0.  0. 14. 14.  1.  0.  0.  0.]\n",
      " [ 0.  0. 14. 16. 14.  4.  0.  0.]\n",
      " [ 0.  1. 16. 16.  8. 16.  2.  0.]\n",
      " [ 0.  0. 14. 11.  0. 13.  9.  0.]\n",
      " [ 0.  0.  9. 14.  6. 16.  7.  0.]\n",
      " [ 0.  0.  0. 14. 16. 14.  0.  0.]]\n"
     ]
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAA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cRytX79e77zzjv72t78pPz/f60lpcxxHyWTS6xn9Wrp0qdra2tTa2po6ioqKtHr1arW2tmr06NFeTxyQZDKpTz/9VDk5OV5P6dfixYuv+LL7zz77THl5eR4tcq+urk7Z2dlavny5pzuGxVtkVVVVWrNmjYqKilRcXKza2lp1dHSovLzc62n9unTpkk6cOJF6fOrUKbW2tmr8+PGaMmWKh8v6V1FRoR07dujdd99VZmZm6u4xHA7r1ltv9XjdtT3//PMqLS1VJBJRV1eX6uvrdejQIe3bt8/raf3KzMy84vOtsWPHasKECTf1517PPvusVqxYoSlTpqizs1O/+tWvlEgkVFZW5vW0fj3zzDNatGiRNm3apO9///v65z//qdraWtXW1no9bUB6e3tVV1ensrIyBYMe/yvek69dM/D66687eXl5TkZGhnPvvff64ktmDx486Ei64igrK/N6Wr+utlmSU1dX5/W0fq1bty71d2TixInO0qVLnffff9/rWWnxw5cpP/74405OTo5zyy23OLm5uc53vvMd5+jRo17PGpD33nvPKSgocEKhkDNjxgyntrbW60kDtn//fkeSc+zYMa+nOPy4fgCACd9/BgMAuDkRGACACQIDADBBYAAAJggMAMAEgQEAmCAwAAATBAYAYILAAABMEBgAgAkCAwAwQWAAACb+B8Vgt2juteCnAAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "标记数字为： 6\n"
     ]
    }
   ],
   "source": [
    "\n",
    "    \n",
    "num_rand = np.random.randint(6,X.shape[0])\n",
    "print(X[num_rand].reshape(8, 8))\n",
    "plt.imshow(X[num_rand].reshape(8, 8),cmap='gray')\n",
    "plt.show()\n",
    "print('标记数字为：',y[num_rand])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "71b41c06",
   "metadata": {},
   "source": [
    "# 3. 将数据集分为测试集和训练集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "id": "2e436d1f",
   "metadata": {},
   "outputs": [],
   "source": [
    "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1afb1c64",
   "metadata": {},
   "source": [
    "# 4. 训练模型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "id": "df80d885",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "KNeighborsClassifier()"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 创建KNN分类器，这里k值设为3\n",
    "knn = KNeighborsClassifier(n_neighbors=5)\n",
    "# 训练模型\n",
    "knn.fit(X_train, y_train)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bece01ae",
   "metadata": {},
   "source": [
    "# 5. 预测测试集，获取测试集的准确率"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "id": "6c6999c5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "模型准确率: 0.9925925925925926\n"
     ]
    }
   ],
   "source": [
    "# 进行预测\n",
    "\n",
    "y_pred = knn.predict(X_test)\n",
    "# 计算准确率\n",
    "accuracy = accuracy_score(y_test, y_pred)\n",
    "print(f\"模型准确率: {accuracy}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "af11df85",
   "metadata": {},
   "source": [
    "# 6. 随机选取数据，并进行预测展示"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "id": "11717dc6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "随机选择的数字为：\n"
     ]
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAZgAAAGdCAYAAAAv9mXmAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjUuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8qNh9FAAAACXBIWXMAAA9hAAAPYQGoP6dpAAAYUElEQVR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WVq2bJmnO4bEW2SVlZVas2aNioqKVFxcrJqaGrW1tam8vNzrab26dOmSTp48mXp8+vRptba2auzYsZo0aZKHy3pXUVGhnTt3as+ePcrIyEjdPYbDYY0aNcrjddf23HPPqbS0VJFIRB0dHaqrq9OhQ4e0f/9+r6f1KiMj46rPt0aPHq1x48bd0J97Pf3001q+fLkmTZqk9vZ2/fznP1cikVBZWZnX03r11FNPaf78+dqyZYu+853v6O9//7tqampUU1Pj9bQ+6e7uVm1trcrKyhQMevxHvCdfu2bglVdecXJzc50RI0Y499xzjy++ZPbtt992JF11lJWVeT2tV1+1WZJTW1vr9bRerV27NvV7ZPz48c6SJUuct956y+tZafHDlyk//PDDTnZ2tnPLLbc4OTk5zje/+U3n2LFjXs/qkz/96U9OQUGBEwqFnKlTpzo1NTVeT+qzAwcOOJKc48ePez3F4dv1AwBM+P4zGADAjYnAAABMEBgAgAkCAwAwQWAAACYIDADABIEBAJggMAAAEwQGAGCCwAAATBAYAIAJAgMAMPE/97m205XVPggAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "标记数字为： 9\n",
      "预测数字为： 9\n"
     ]
    }
   ],
   "source": [
    "\n",
    "rd_num = np.random.randint(0,X_test.shape[0])\n",
    "X_now = X_test[rd_num]\n",
    "y_now = y_test[rd_num]\n",
    "y_predict = knn.predict(X_now.reshape(-1,64))\n",
    "print('随机选择的数字为：')\n",
    "\n",
    "plt.imshow(X_now.reshape(8, 8),cmap='gray')\n",
    "plt.show()\n",
    "print('标记数字为：',y_now)\n",
    "print('预测数字为：',y_predict[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "406b4ed7",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0ae10ce0",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
